package org.apm.data.check;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apm.data.check.DataReIndex.DataBean;
import org.apm.data.pusher.CommonMethod;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import com.alibaba.fastjson.JSONObject;

public class RelationShipReIndex extends DataReIndex {
    private static final Logger LOGGER = LogManager.getLogger(RelationShipReIndex.class);
    private static final String TYPE = "RelationShip";

    public RelationShipReIndex() {}

    public RelationShipReIndex(Client client, BulkProcessor esBulkProcessor) {
        super(client, esBulkProcessor);
    }

    @Override
    protected boolean checkIndex(String index) {
        return index.contains(TYPE.toLowerCase());
    }

    @Override
    protected QueryBuilder query() {
        return QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("dstType", 11))
                .filter(QueryBuilders.termsQuery("dst", "127.0.0.1:3306", "tomcat_cms@101.37.82.175:8130:3306"));
    }

    @Override
    protected DataBean processData(String dataSource) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(dataSource);
            jsonObject.put("dst", "101.37.82.175:3306");
            return new DataBean(jsonObject.toJSONString(), true);
        } catch (Exception exception) {
            LOGGER.error(exception.getMessage());
            exception.printStackTrace();
        }
        return new DataBean();
    }

    public static void main(String[] args) {
        DataReIndex dataReIndex = new RelationShipReIndex(CommonMethod.onlineClient(), CommonMethod.EsBulkProcessor.ONLINE.processor());
        dataReIndex.copyIndexData("apm2.0-fintech-relationship-1", "apm2.0-fintech-relationship-1");
    }

}
